package com.alex.goods.controller;

import com.alex.goods.service.AdminService;
import com.alex.goods.service.impl.AdminServiceImpl;
import com.alex.goods.util.MD5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		AdminService service = new AdminServiceImpl();
		Boolean right = service.check(username, MD5Util.getMD5(password));
		if (right) {
			request.getSession().setAttribute("currentname", username);
			response.sendRedirect("/admin?action=page");
		} else {
//			Result result = new Result();
//			result.setCode(-1);
//			result.setMsg("用户名或密码错误");
//			PrintWriter out = response.getWriter();
//			out.write(JSON.toJSONString(result));
//			out.flush();
//          out.close();
			response.sendRedirect("/login");
//			request.setAttribute("msg","账号或密码错误");
//			request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);

		}
	}
}
